/*
* @Author: chenjianghui
* @Date:   2018-09-03 13:47:48
* @Last Modified by:   chenjianghui
* @Last Modified time: 2018-09-11 17:17:52
*/
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manager/recharge/index',
                    add_url: 'manager/recharge/add',
                    edit_url: 'manager/recharge/edit',
                    del_url: 'manager/recharge/del',
                    multi_url: 'manager/recharge/multi',
                    ajax_url:{
                        btn_1:"manager/recharge/change_balance"
                    },
                    btn_mall_window:{
                        btn_array:["立即提交","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function () {
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['600px','400px']
                    },
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            // table.on('post-body.bs.table', function (e, json) {
            //     $("tbody tr[data-index]", this).each(function () {
            //         if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
            //             $("input[type=checkbox]", this).prop("disabled", true);
            //         }
            //     });
            // });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'username', title: __('Username'), operate:false},
                        {field: 'username_no', title: __('Nickname'), operate:false},
                        {field: 'status', title: __("Status"), operate:false},
                        {field: 'group', title: __('Group'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'platform', title: __('Platform')},
                        {field: 'balance', title: __('Balance'), operate:false},
                        {field: 'recharge_money', title: __('Recharge_money')},
                        {field: 'recharge_image', title: __('Recharge_image'), formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'recharge_status', title: __('Recharge_status'), operate:false},
                        // {
                        //     field: 'mimetype', title: __('Mimetype'), operate: 'LIKE %...%',
                        //     process: function (value, arg) {
                        //         return value.replace(/\*/g, '%');
                        //     }
                        // },
                        // {field: 'url', title: __('Recharge_image'), formatter: Controller.api.formatter.thumb, operate: false},
                        // {field: 'url', title: __('Url'), formatter: Controller.api.formatter.url},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            // table: table,buttons: [
                            //     {name: 'name2', text: '修改余额', title: '修改余额', icon: 'fa fa-flash', classname: 'btn btn-xs btn-success btn-dialog', url: 'manager/recharge/change_balance', success:function(data, ret){ console.log("aaa");$(table).bootstrapTable("refresh")}, error:function(){}},
                            // ],
                            // events: Table.api.events.operate, formatter: function (value, row, index) {
                            //     console.log(row);
                            //     $(table).data("operate-del", null);
                            //     $(table).data("operate-edit", null);
                            //     return Table.api.formatter.operate.call(this, value, row, index);
                            // }
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                if (row.recharge_status == '待充值') {
                                    buttons.push({
                                        name: 'name2',
                                        text: '修改余额',
                                        title: '修改余额',
                                        icon: 'fa fa-flash',
                                        extend: 'data-toggle="tooltip" data-name="btn_1" data-title="修改余额"',
                                        classname: 'btn btn-xs btn-success btn-dialog btn-small-window'
                                    });
                                    return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                                }
                            }
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        change_balance: function () {
            Controller.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    //if (row.mimetype.indexOf("image") > -1) {
                        var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    // } else {
                    //     return '<a href="' + row.fullurl + '" target="_blank">' + __('None') + '</a>';
                    // }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            }
        }
    };
    return Controller;
});